package com.bolt.admin.web.controller;

import com.bolt.admin.common.base.BaseController;
import com.bolt.admin.logging.annotation.LogAnnotation;
import com.bolt.admin.module.sys.dto.RoleDTO;
import com.bolt.admin.module.sys.dto.converter.RoleConverter;
import com.bolt.admin.module.sys.entity.PrivilegeEntity;
import com.bolt.admin.module.sys.entity.RoleEntity;
import com.bolt.admin.module.sys.service.RoleService;
import com.bolt.common.reflect.ObjectUtil;
import com.bolt.convention.data.PageInfo;
import com.bolt.convention.data.ResultMessage;
import com.bolt.convention.data.Returns;
import com.bolt.core.view.components.Form;
import com.bolt.support.spring.jpa.specification.QueryHelp;
import com.bolt.support.spring.mvc.annotation.ModelEntity;
import com.bolt.support.ui.tree.vo.UITreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * Auto Generate Javadoc
 *
 * @author 日期:2018-02-26 17:21:27
 *         角色信息
 **/
@Controller
@RequestMapping("/admin/sys/role")
public class RoleController extends BaseController <RoleEntity, String> {

    @Autowired
    private RoleService roleService;


    /**
     * 模块首页
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView index(Model model, HttpServletRequest request) {
        return super.index("/admin/sys/role/index", request, model.asMap());
    }


    /**
     * 角色列表分页
     */
    @RequestMapping(value = "page", method = RequestMethod.POST)
    @ResponseBody
    public ResultMessage list(Pageable pageable, RoleDTO roleDTO) {
        PageInfo <RoleEntity> pageInfo = roleService.findPage(pageable,
                (root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, roleDTO, criteriaBuilder));
        return Returns.success(pageInfo.map(K -> RoleConverter.toDTO(K)));
    }


    /**
     * 角色信息新增操作
     *
     * @return
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String create() {
        return "/admin/sys/role/input-index";
    }

    /**
     * 角色信息编辑页
     *
     * @param entity 菜单ID
     * @param model
     * @return
     */
    @RequestMapping(value = "edit", method = RequestMethod.GET)
    public String edit(@ModelEntity(Form.PAGECONTEXT_FORMVALUES) RoleEntity entity, Model model) {
        return "/admin/sys/role/input-index";
    }

    /**
     * 角色信息编辑操作
     *
     * @param dto 表单数据
     * @return
     */
    @RequestMapping(value = "saveOrUpdate", method = RequestMethod.POST)
    @ResponseBody
    @LogAnnotation(module = "系统管理", description = "角色信息编辑操作")
    public ResultMessage saveOrUpdate(@Validated RoleDTO dto) {
        roleService.saveOrUpdate(dto);
        return Returns.success("保存成功");
    }

    /**
     * 角色信息删除操作
     *
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(value = "del", method = RequestMethod.GET)
    @ResponseBody
    @LogAnnotation(module = "系统管理", description = "角色信息删除操作")
    public ResultMessage doDelete(@RequestParam("id") String id) {
        if (ObjectUtil.isNotNull(id)) {
            roleService.deleteById(id);
            return Returns.success();
        } else {
            return Returns.error("所有选取记录删除操作失败");
        }
    }


    /**
     * 分配角色权限资源
     *
     * @return
     */
    @RequestMapping(value = "privileges", method = RequestMethod.GET)
    public String privileges() {
        return "/admin/sys/role/role_privileges";
    }

    /**
     * 权限资源设置
     *
     * @param roleId
     * @param checkMenuIds
     * @param cancelItems
     * @param currentMenuId
     * @return
     */
    @RequestMapping(value = "/privileges", method = RequestMethod.POST)
    @ResponseBody
    @LogAnnotation(module = "系统管理", description = "权限资源设置")
    public ResultMessage privilegeR2sSave(
            @RequestParam(value = "roleId", required = true)
                    String roleId,
            @RequestParam(value = "checkMenuIds", required = false)
                    Long[] checkMenuIds,
            @RequestParam(value = "cancelItems", required = false)
                    String[] cancelItems,
            @RequestParam(value = "currentMenuId", required = false)
                    Long currentMenuId) {
        try {
            roleService.updateRelatedPrivilege(roleId, checkMenuIds, cancelItems, currentMenuId);
            return Returns.success("设置成功");
        } catch (Exception ex) {
            logger.error(ex);
            return Returns.error(ex.toString());
        }
    }

    @RequestMapping(value = "/selectButton", method = RequestMethod.GET)
    @ResponseBody
    public List <PrivilegeEntity> findButtonByMenuId(@RequestParam(value = "roleId")
                                                             String roleId,
                                                     @RequestParam(value = "menuId")
                                                             Long menuId) {
        return roleService.findRoleButtonByMenuId(roleId, menuId);
    }


    /**
     * 取出权限关联菜单列表
     *
     * @param roleId
     * @return
     */
    @RequestMapping(value = "getMenuSimpleTree")
    @ResponseBody
    public List <UITreeNode> getMenuList(@RequestParam(name = "id", required = false) String roleId) {
        return roleService.getSimpleMenuTree(roleId);
    }

    /**
     * 查找所有角色ID与名称
     *
     * @return
     */
    @RequestMapping(value = "/getAllRole", method = RequestMethod.GET)
    @ResponseBody
    public ResultMessage getAllRole() {
        List <RoleDTO> data = roleService.getAllRoleData();
        return Returns.success(data);
    }

}